<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\Order;

use App\Http\Model\Home_user;

use App\Http\Model\Goods;

class OrderController extends Controller
{
    
    public function index(Request $request)
    {

       
        //dd($request->all());
        $count = $request -> input('count',3);

        // dd($count);
        
        $search = $request -> input('search','');
        $request = $request -> all();

        $order = new Order();

        $data = $order->where('name','like','%'.$search.'%')
                    ->join('sad_home_user','sad_order.uid','=','sad_home_user.id')
                    ->join('sad_goods','sad_order.gid','=','sad_goods.id')
                    ->paginate($count);

        
        //加载订单显示页面
        return view('admin.order.index',compact('data','request'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

         $order = new Order();
        
        $data = $order->join('sad_home_user','sad_order.uid','=','sad_home_user.id')
                    ->first()
                    ->toArray();



                $str ='<table class="table table-striped table-bordered table-hover table-full-width dataTable" id="sample_2" aria-describedby="sample_2_info">
                
                <tr>
                    <th>用户昵称</th>
                    <th>创建时间</th>
                    <th>用户电话</th>
                    <th>用户邮箱</th>
                </tr>';


            // foreach($data as $k=>$v)
            // {
            $str.= "<tr>
                        <td class='sorting_1''>{$data['username']}</td>
                        <td class='sorting_1'>{$data['ctime']}</td>
                        <td class='sorting_1'>{$data['phone']}</td>
                        <td class='sorting_1'>{$data['email']}</td>
                    </tr>";

            // }

            $str.= '</table>';

            return $str;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {


        return view('admin.order.edit',compact('id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {   

        //处理修改页面提交过来的数据
        $status = $request->input('status');

        $order = new Order();

        $gid = $order->where('id',$id)->find('gid');



        $goods = new Goods();


        $affect = $goods->where('id',$id)->update(['status'=>$status]);
        
         if($affect){
            return redirect('/order/');
        }else{
            return back()->with('errors','用户添加失败');
        }


    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function details()
    {


        // $re =  Article::find($id)->delete();
        // $data = [];
        // if($re){
        //     $data['status']= 0;
        //     $data['msg']='删除成功';
        // }else{
        //     $data['status']= 1;
        //     $data['msg']='删除失败';
        // }
        // return $data;

        

   
    }
    
}
